package com.hex.ds.hdtp.core.app.data.contrast.service.impl;

import com.hex.ds.hdtp.core.app.data.contrast.service.IContrastTaskTemplateService;
import com.hex.ds.hdtp.core.inf.data.contrast.po.ContrastTaskTemplatePo;
import com.hex.ds.hdtp.core.inf.data.contrast.repository.IContrastTaskTemplateRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.app.data.contrast.service.impl
 * @ClassName ContrastTaskTemplateService
 * @Description: 跨库比对任务与模板关系-业务接口实现
 * @Author: wz.li
 * @Date 2023/7/25 19:39
 * @Version v2.0.2
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ContrastTaskTemplateService implements IContrastTaskTemplateService {

    private final IContrastTaskTemplateRepository contrastTaskTemplateRepository;

    /*
     * @Method: add <br>
     * @Param: [taskPkId, templatePkId] <br>
     * @Return: int <br>
     * @Description：新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/25 19:41 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    @Transactional
    public int add(String taskPkId, String templatePkId) {
        ContrastTaskTemplatePo contrastTaskTemplatePo = new ContrastTaskTemplatePo()
                .setTaskPkId(taskPkId)
                .setTemplatePkId(templatePkId);
        return contrastTaskTemplateRepository.add(contrastTaskTemplatePo);
    }

    /**
     * @Method batchAddList
     * @Param
     * @param taskPkId
     * @param templatePkIds
     * @Return int
     * @Description 新增
     * @Author Wang zhihao
     * @Date 2023/8/1 19:18
     * @Version V1.0
     */
    @Override
    public int batchAddList(String taskPkId, List<String> templatePkIds) {
        Integer count = 0;
        for (String templatePkId : templatePkIds) {
            ContrastTaskTemplatePo contrastTaskTemplatePo = new ContrastTaskTemplatePo()
                    .setTaskPkId(taskPkId)
                    .setTemplatePkId(templatePkId);
            count = contrastTaskTemplateRepository.add(contrastTaskTemplatePo);
        }
        return count;
    }

    /**
     * @Method rebuildById
     * @Param
     * @param taskPkId
     * @param templatePkIds
     * @Return void
     * @Description 根据主键重建任务与模板关系
     * @Author Wang zhihao
     * @Date 2023/8/25 15:13
     * @Version V1.0
     */
    @Override
    public void rebuildById(String taskPkId, String templatePkId) {
        contrastTaskTemplateRepository.removeByTaskPkId(taskPkId);
        add(taskPkId, templatePkId);
    }

    /*
     * @Method: removeByTaskPkId <br>
     * @Param: [taskPkId] <br>
     * @Return: java.lang.Integer <br>
     * @Description：根据任务主键删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/25 19:41 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Integer removeByTaskPkId(String taskPkId) {
        return contrastTaskTemplateRepository.removeByTaskPkId(taskPkId).equals(0) ? 0 : 1;
    }

    /**
     * @Method queryTemplatePkIdByTaskPkId
     * @Param
     * @param taskPkId
     * @Return java.util.List<java.lang.String>
     * @Description 根据任务主键查询模板主键
     * @Author Wang zhihao
     * @Date 2023/7/31 13:45
     * @Version V1.0
     */
    @Override
    public List<String> queryTemplatePkIdByTaskPkId(String taskPkId) {
        return contrastTaskTemplateRepository.queryTemplatePkIdByTaskPkId(taskPkId);
    }
}
